/*
 * Created on 08/24/2011 - 07:30 AM
 */
package sembr.geometry.interfacing;

import importadorDXF.*;
import sembr.geometry.shapes.ShapesCollection;

/**
 *
 * @author Adriano
 */
public class DxfDataSetExporter extends DataSetExporter
{
    DxfReader dxfReader; 
    ShapesCollection shapesCollector;
        
    public DxfDataSetExporter(String dxfFilePath)
    {
        this.dxfReader = new DxfReader(dxfFilePath);
        this.shapesCollector = new ShapesCollection();
        this.setShape();
        this.setShapesByEntitiesCollectors();
    }
    
    private void setShape()
    {
        DxfEntity currentEntity;
        
        if (hasBlock())
        {
            int nrOfBlocks = this.getNrOfBlockEntities();
            
            for (int i = 0; i < nrOfBlocks; i++)
            {
                DxfBLOCK block = this.getCurrentBlock(i);
                
                int nrOfEntitiesPerBlock = block.getEntities().getNrOfEntities();
                
                for (int j = 0; j < nrOfEntitiesPerBlock; j++)
                {
                    currentEntity = block.getEntities().getEntity(j);
                    
                    this.SetShapeByEntity(currentEntity);
                }
            }
        }
        
        int nrOfEntities = this.getNrOfEntities();
        
        for (int k = 0; k < nrOfEntities; k++)
        {
            currentEntity = this.getCurrentEntity(k);
            
            this.SetShapeByEntity(currentEntity);
        }         
    }
    
    private void SetShapeByEntity(DxfEntity entity)
    {
        if (entity.getClass() == Dxf3DFACE.class)
        {
            shapesCollector.getFaceCollector().add((Dxf3DFACE)entity);
        }
        
        else if (entity.getClass() == DxfPOLYLINE.class)
        {
            // Polyline has a variant called Polyface
            // So these are put in different collectors
            DxfPOLYLINE currentPolyline = (DxfPOLYLINE)entity;
            
            if (currentPolyline.getNrCtrlU() > 0 && 
                currentPolyline.getNrCtrlV() > 0 &&
                currentPolyline.getType() == 64)
                // for polyface object
                shapesCollector.getPolyfaceCollector().add((DxfPOLYLINE)entity);
            else
                // for polyline object
                shapesCollector.getPolylineCollector().add((DxfPOLYLINE)entity);                
        }
        
        else if (entity.getClass() == DxfLWPOLYLINE.class)
        {
            shapesCollector.getLwPolylineCollector().add((DxfLWPOLYLINE)entity);
        }
        
        else if (entity.getClass() == DxfLINE.class)
        {
            shapesCollector.getLineCollector().add((DxfLINE)entity);
        } 
        
        else if(entity.getClass() == DxfCIRCLE.class)
        {
            shapesCollector.getCircleCollector().add((DxfCIRCLE)entity);
        }
        
        else if (entity.getClass() == DxfARC.class)
        {
            shapesCollector.getArcsCollector().add((DxfARC)entity);
        }
        
        else if(entity.getClass() == DxfINSERT.class)
        {
            shapesCollector.getInsertCollector().add((DxfINSERT)entity);
        }
        
        else
        {
            shapesCollector.getEntityBaseCollector().add(entity);
        }
    } 
    
    private void setShapesByEntitiesCollectors()
    {
        // add not empty shape collection
        if (!shapesCollector.getFaceCollector().getElements().isEmpty())
        {
            super.add(shapesCollector.getFaceCollector());
        }
        
        if (!shapesCollector.getPolylineCollector().getElements().isEmpty())
        {
            super.add(shapesCollector.getPolylineCollector());
        }
        
        if (!shapesCollector.getPolyfaceCollector().getElements().isEmpty())
        {
            super.add(shapesCollector.getPolyfaceCollector());
        }
        
        if (!shapesCollector.getLwPolylineCollector().getElements().isEmpty())
        {
            super.add(shapesCollector.getLwPolylineCollector());
        }
        
        if (!shapesCollector.getLineCollector().getElements().isEmpty())
        {
            super.add(shapesCollector.getLineCollector());
        }
        
        if (!shapesCollector.getCircleCollector().getElements().isEmpty())
        {
            super.add(shapesCollector.getCircleCollector());
        }
        
        if (!shapesCollector.getArcsCollector().getElements().isEmpty())
        {
            super.add(shapesCollector.getArcsCollector());
        }
        
        if (!shapesCollector.getInsertCollector().getElements().isEmpty())
        {
            super.add(shapesCollector.getInsertCollector());
        }
        
        if (!shapesCollector.getEntityBaseCollector().getElements().isEmpty())
        {
            super.add(shapesCollector.getEntityBaseCollector());
        }
    }
    
    private boolean hasBlock()
    {
        return this.getNrOfBlockEntities() != 0 ? true : false;  
    }    
    
    private DxfBLOCK getCurrentBlock(int index)
    {
        return (DxfBLOCK)this.dxfReader.getDxfFile().getBlocks().getBlockSet().getEntity(index);
    }
    
    private int getNrOfBlockEntities()
    {
        int nrOfBlockEntities = 0;
        DxfFile dxfFile = this.dxfReader.getDxfFile();
                
        if (this.dxfReader.getDxfFile().getBlocks() != null)     
        {
            nrOfBlockEntities = this.dxfReader.getDxfFile().getBlocks().getBlockSet().getNrOfEntities();
        }
        
        return nrOfBlockEntities;
    }  
    
     private DxfEntity getCurrentEntity(int index)
    {
        return this.dxfReader.getDxfFile().getEntities().getEntitySet().getEntity(index);                
    }
    
    private int getNrOfEntities()
    {       
        return            
            this.dxfReader.getDxfFile().getEntities().getEntitySet().getNrOfEntities();
    }
    
    private int getNrOfEntitiesAtAll()
    {
        return getNrOfEntities() + getNrOfBlockEntities();
    }   
    
//    public static void main(String[] args) 
//    {
//        DxfDataSetExporter dxfDataSetExporter = new DxfDataSetExporter("C:\\Projects\\sembr.dxf.importer\\yate01.dxf");
//    }
}
